import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { EventBusService } from 'src/app/common/event-bus.service';
import { HttpClient } from '@angular/common/http';
import { res } from 'src/app/common/Interface';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
  selector: 'app-commodity-management',
  templateUrl: './commodity-management.component.html',
  styleUrls: ['./commodity-management.component.less'],
})
export class CommodityManagementComponent implements OnInit {
  constructor(
    private router: Router,
    private bus: EventBusService,
    private http: HttpClient,
    private message: NzMessageService
  ) {}

  condition = {
    pageNum: 1,
    pageSize: 10,
    keywords: '',
    state: 1,
  };

  getupOrDown(state) {
    this.condition.state = state;
    // this.
    this.getList();
  }

  getList() {
    this.http
      .post(`/commodity/list`, this.condition)
      .subscribe(({ code, data, msg }: res) => {
        if (code == 200) {
          this.listOfData = data.data;
          this.total = data.total;
        }
      });
  }
  ngOnInit(): void {
    this.getList();
    this.bus.getListBus.subscribe((res) => {
      this.getList();
    });
  }
  listOfData = [];
  total = 0;
  searchDown(evt) {
    if (evt.keyCode == 13) {
      this.getList();
    }
  }
  detail(data: any) {
    console.log(data);
    this.router.navigateByUrl('main/detail/' + data.commodity_id);
  }

  clickMe(data: any, type: string) {
    type == 'out' ? this.setOutForms(data) : this.setInForms(data);
    data.show = false;
  }
  showAdd() {
    this.bus.bus.next();
  }

  onBlur(evt, key, obj): void {
    this[obj][key] = Math.round(parseFloat(evt.target.value) * 100) / 100;
  }

  outforms = {
    num: NaN,
    price: NaN,
    customer: '',
    count_price: NaN,
    commodity_name: '',
    reserve: NaN,
    commodity_id: 0,
  };
  setOutForms(data) {
    const { commodity_id, commodity_name, price, num } = data;
    this.outforms = {
      ...this.outforms,
      commodity_id,
      commodity_name,
      price,
      reserve: num,
      num: NaN,
      customer: '',
      count_price: NaN,
    };
    this.showOutWindow = true;
  }

  informs = {
    in_num: 0,
    in_price: 0.0,
    dealer: '',
    payment: 0,
    commodity_id: 0,
    commodity_name: '',
  };
  setInForms(data) {
    const { commodity_id, commodity_name } = data;
    this.informs = {
      in_num: NaN,
      in_price: NaN,
      dealer: '',
      payment: 0,
      commodity_id,
      commodity_name,
    };
    this.showInWindow = true;
  }

  ngNumChange(n, obj = '') {
    if (obj == 'informs') {
      const count_price = (parseInt(n) || 0) * this.informs.in_price;
      this.informs.payment = count_price;
    } else {
      const count_price = (parseInt(n) || 0) * this.outforms.price;
      this.outforms.count_price = count_price;
    }
  }
  showOutWindow = false;
  showInWindow = false;

  addOut() {
    this.http
      .post('/outLog/add', this.outforms)
      .subscribe(({ code, msg, data }: res) => {
        if (code == 200) {
          this.message.success(msg);
          this.showOutWindow = false;
          this.getList();
        } else {
          this.message.error(msg);
        }
      });
  }
  addIn() {
    this.http
      .post('/inLog/add', this.informs)
      .subscribe(({ code, msg, data }: res) => {
        if (code == 200) {
          this.message.success(msg);
          this.showInWindow = false;
          this.getList();
        } else {
          this.message.error(msg);
        }
      });
  }
}
